﻿namespace LastVersionOfIpPackage.GUI
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    /// <summary>
    /// smoothing filter form
    /// </summary>
    public partial class SmoothingFilter : Form
    {
        /// <summary>
        /// type of filter
        /// </summary>
        private Enum_needed.SmoothingType type;

        /// <summary>
        /// flag to known if it save in orginal or not
        /// </summary>
        private bool saveOrNot = false;

        /// <summary>
        /// the modified image
        /// </summary>
        private Bitmap resultImg;

        /// <summary>
        /// Initializes a new instance of the <see cref="SmoothingFilter"/> class.
        /// </summary>
        public SmoothingFilter()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Gets or sets a value indicating whether [save or not].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [save or not]; otherwise, <c>false</c>.
        /// </value>
        public bool SaveOrNot
        {
            get { return this.saveOrNot; }
            set { this.saveOrNot = value; }
        }

        /// <summary>
        /// Gets or sets the result img.
        /// </summary>
        /// <value>
        /// The result img.
        /// </value>
        public Bitmap ResultImg
        {
            get { return this.resultImg; }
            set { this.resultImg = value; }
        }

        /// <summary>
        /// Sets the original image info.
        /// </summary>
        /// <param name="org">The org.</param>
        /// <param name="type">The type.</param>
        public void SetInfo(Image org, Enum_needed.SmoothingType type)
        {
            this.picbOrignal.Image = org;
            this.type = type;
            if (this.type == Enum_needed.SmoothingType.AlphaTrimmedMean)
            {
                this.grpD.Visible = true;
            }

            this.label1.Text = type.ToString();
        }

        /// <summary>
        /// Handles the Click event of the BtnCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Handles the Click event of the BtnApply control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnApply_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.type == Enum_needed.SmoothingType.AlphaTrimmedMean)
                {
                    this.resultImg = ImageOperations.ImageEnchance.NewImageEnchanemwnt.SmoothingFilter((Bitmap)this.picbOrignal.Image, double.Parse(this.txtbMaskSize.Text), int.Parse(this.txtD.Text), this.type);
                }
                else
                {
                    this.resultImg = ImageOperations.ImageEnchance.NewImageEnchanemwnt.SmoothingFilter((Bitmap)this.picbOrignal.Image, double.Parse(this.txtbMaskSize.Text), this.type);
                }

                this.picbModified.Image = this.resultImg;
                this.BtnSave.Enabled = true;
            }
            catch
            {
                if (this.picbOrignal.Image == null)
                {
                    MessageBox.Show("no image to make smoothing in it operation");
                }
                else
                {
                    MessageBox.Show("operation fail make sure your data");
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the BtnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnSave_Click(object sender, EventArgs e)
        {
            if (this.picbModified.Image != null)
            {
                this.saveOrNot = true;
                this.Close();
            }
        }
    }
}
